﻿using CloudMusic.Helper;
using CloudMusic.Model;
using CloudMusic.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CloudMusic.View
{
    /// <summary>
    /// PageAlbumInfo.xaml 的交互逻辑
    /// </summary>
    public partial class PageAlbumInfo : Page
    {
        public PageAlbumInfo(PageAlbumInfoViewModel pavm)
        {
            InitializeComponent();
            this.DataContext = pavm;
        }
        private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            CommonHelper.RunScorllView(sender, e);
        }

        private void ItemIist_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (e.Row.GetIndex() < 9)
            {
                e.Row.Header = "0" + (e.Row.GetIndex() + 1);
            }
            else
            {
                e.Row.Header = e.Row.GetIndex() + 1;
            }
        }

        /// <summary>
        /// 跳转到歌手详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SingerDetail_GotFocus(object sender, MouseButtonEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            long ablumId = music.SingerId;
            var ps = new PageSingerInfoViewModel(ablumId);
            PageSingerInfo pageSingerInfo = new PageSingerInfo(ps);
            NavigationService.Navigate(pageSingerInfo);
        }
        /// <summary>
        /// 双击改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemIist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            var item = ObjectToEntityHelper.ConvertObjectByJson<PlayModel>((ObservableCollection<MusicModel>)ItemIist.ItemsSource);
            if (!PlayListHelper.MainMusicList.Any(t => t.Id == music.Id))
            {
                PlayListHelper.MainMusicList.Clear();
                item.ForEach(p => PlayListHelper.MainMusicList.Add(p));
            }
        }
        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            MusicViewModelBase.songListEvent += MusicViewModelBase_songListEvent;
        }

        private void MusicViewModelBase_songListEvent(object sender, EventArgs e)
        {
            long id = (long)sender;
            if ((ObservableCollection<MusicModel>)ItemIist.ItemsSource != null)
            {
                var item = ObjectToEntityHelper.ConvertObjectByJson<PlayModel>((ObservableCollection<MusicModel>)ItemIist.ItemsSource);
                if (!PlayListHelper.MainMusicList.Any(t => t.Id == id))
                {
                    item.ForEach(p => PlayListHelper.MainMusicList.Add(p));
                }
            }
        }
    }
}
